/*
 * $Id: OpenedResultAdapterFactory.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.model;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.OverlayIcon;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * Factory for creating IOpenedResult adapter classes.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class OpenedResultAdapterFactory implements IAdapterFactory
{
	private IWorkbenchAdapter adapter = new OpenedResultAdapter();
	
	/**
	  * Convenience method for creating new instances of
	  * OpenedResultAdapterFactory.
	  */
	public static OpenedResultAdapterFactory newInstance()
	{
		return new OpenedResultAdapterFactory();
	}

	/** Creates a new OpenedResultAdapterFactory instance. */
	private OpenedResultAdapterFactory() { }		
	
	/** @see IAdapterFactory#getAdapter(Object, Class) */
	public Object getAdapter(Object adaptableObject, Class adapterType)
	{
		if ((adapterType == IWorkbenchAdapter.class) && 
		    (adaptableObject instanceof IOpenedResult))
		{
			return adapter;
		}
		
		return null;
	}

	/** @see IAdapterFactory#getAdapterList() */
	public Class[] getAdapterList() 
	{ 
		return new Class[]{ IWorkbenchAdapter.class }; 
	}
	
	/**
	  * Registers the adapter objects created by this factory with the
	  * Platform plugin.
	  */
	public void register()
	{
		Platform.getAdapterManager().registerAdapters(this, IOpenedResult.class);
	}	
	
	/**
	  * Adapter class for IOpenedResult implementations.
	  */
	private static class OpenedResultAdapter implements IWorkbenchAdapter
	{
		/** @see IWorkbenchAdapter#getChildren(Object) */
		public Object[] getChildren(Object o) 
		{ 
			if (!(o instanceof IOpenedResult))
				return null; 
								
			return new Object[0];
		}

		/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
		public ImageDescriptor getImageDescriptor(Object o) 
		{ 
			if (!(o instanceof IOpenedResult))
				return null; 

			IInfoResult infoResult = PerforceUIPlugin.getClient().info();
			
			IOpenedResult openedResult = (IOpenedResult)o;			

			int start = openedResult.getDepotFilename().lastIndexOf("/");
			String filename = openedResult.getDepotFilename().substring(start);
			
			ImageDescriptor result = PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(filename);

			List overlayList = new ArrayList(1);
			if (infoResult.getClientName().equals(openedResult.getClientName()))
			{				
				if (openedResult.getAction().equals("edit"))
					overlayList.add(PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.OVR_OPENFOREDIT));	
				else
				if (openedResult.getAction().equals("add"))
					overlayList.add(PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.OVR_OPENFORADD));		
			}
			else
			{
				if (openedResult.getAction().equals("edit"))
					overlayList.add(PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.OVR_OTHER_OPENFOREDIT));						
			}
			
			return  result;
			/*
			ImageDescriptor[][] overlays = new ImageDescriptor[][] {(ImageDescriptor[]) overlayList.toArray(new ImageDescriptor[overlayList.size()])};
			return new OverlayIcon(result.createImage().getImageData(), overlays, new Point(16, 16));				
			*/
		}

		/** @see IWorkbenchAdapter#getLabel(Object) */
		public String getLabel(Object o)
		{ 
			if (!(o instanceof IOpenedResult))
				return null; 
			
			IOpenedResult openedResult = (IOpenedResult)o;			

			Object[] arguments = new Object[4];
			arguments[0] = openedResult.getDepotFilename();
			arguments[1] = openedResult.getRevision();
			arguments[2] = openedResult.getType();
			arguments[3] = openedResult.getAction();

			return MessageFormat.format("{0}#{1} <{2}> <{3}>", arguments);
		}

		/** @see IWorkbenchAdapter#getParent(Object) */
		public Object getParent(Object o) { return null; }

	}
}
